package com.clickpaas.workinghours.dao.Impl;

import com.bizcloud.ipaas.tbba4bf83484b4ed2aacba581fbfeac4d.d20210220155135.codegen.LeadApi;
import com.bizcloud.ipaas.tbba4bf83484b4ed2aacba581fbfeac4d.d20210220155135.model.*;
import com.clickpaas.workinghours.bean.WorkingHoursDTOBean;
import com.clickpaas.workinghours.constant.ConstantFile;
import com.clickpaas.workinghours.dao.LeadDao;
import com.clickpaas.workinghours.entity.LeadEntity;
import com.clickpaas.workinghours.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Repository
public class LeadDaoImpl implements LeadDao {

    @Resource
    private WorkingHoursDTOBean dtoBean;
    @Resource
    private LeadApi leadApi;
    /**
     * 根据条件查询
     *
     * @param leadDTO
     */
    private List<LeadDTOResponse> getDataByCondtion(LeadDTO leadDTO) {
        DataResponseListLeadDTO dataInfo = leadApi.findLeadUsingPOST(leadDTO);
        if(null == dataInfo
                || null == dataInfo.getData()
                || dataInfo.getData().size()<=0
                || !"SUCCESS".equals(dataInfo.getMessage())){
            log.info("当前查询的数据：【{}】",dataInfo);
            return null;
        }
        return dataInfo.getData();
    }

    /**
     * 根据条件查询Lead对象数据
     *
     * @param Entity
     */
    @Override
    public List<LeadDTOResponse> queryDataInfo(LeadEntity Entity) {
        LeadDTO leaddto = dtoBean.leaddto();
        try {
            SpringUtil.copyPropertiesIgnoreNull(Entity,leaddto);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",Entity);
        }
        return getDataByCondtion(leaddto);
    }

    /**
     * 线索数据删除
     *
     * @param entity
     */
    @Override
    public boolean deleteDataInfo(LeadEntity entity) {
        LeadDTODelete leadDTODelete = dtoBean.leadDTODelete();
        ArrayList<LeadDTODelete> deleteDatas = new ArrayList<>();
        try {
            SpringUtil.copyPropertiesIgnoreNull(entity,leadDTODelete);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",entity);
        }
        deleteDatas.add(leadDTODelete);
        String message = leadApi.deleteLeadUsingPOST(deleteDatas).getMessage();
        if (ConstantFile._MESSAGE.equals(message)){
            return true;
        }
        return false;
    }

    /**
     * 线索数据入库
     *
     * @param query
     * @param update
     */
    @Override
    public LeadSaveOrUpdateDataResponseObject updateDataInfo(LeadEntity query,LeadEntity update) {
        LeadDTOQuery queryData = dtoBean.leadDTOQuery();
        LeadDTOUpdate updateData = dtoBean.leadDTOUpdate();
        try {
            if (null != query){
                BeanUtils.copyProperties(query,queryData);
            }
            BeanUtils.copyProperties(update,updateData);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象query:【{}】,update:【{}】",query,update);
        }
        SaveOrUpdateLeadDTO saveInfo = new SaveOrUpdateLeadDTO();
        saveInfo.setQuery(queryData);
        saveInfo.setUpdate(updateData);
        return leadApi.saveOrUpdateLeadUsingPOST(saveInfo);
    }


}
